package cn.echoedu.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import cn.echoedu.command.base.ExecuteParam;
import cn.echoedu.command.base.FailureParam;
import cn.echoedu.command.base.SuccessParam;
import cn.echoedu.entity.TelContacts;
import cn.echoedu.util.L;

import com.google.gson.Gson;
public class ContactsListControlCommand extends BaseCommand{
	private final String T="ContactListCommand--------->";
    
	//		"[{\"contactsid\":1,\"name\":\"SONGLUSHENG\",\"tel\":\"18039516436\",\"imageid\":0,\"type\":\"CONTACTSLIST\",\"ishot\":1},{\"contactsid\":2,\"name\":\"songlusheng\",\"tel\":\"18039516436\",\"imageid\":0,

	public void executeSuccess(SuccessParam successparam) {
		// TODO Auto-generated method stub
		List<TelContacts> telcontacts=(List<TelContacts>)successparam.getParam();
		System.out.println("--------111->"+telcontacts);
		  List<ContactsListEntity> listcontact=new ArrayList<ContactsListEntity>();
		ContactsListSuccessResponse contactlistsuccess=new ContactsListSuccessResponse();
	   for(TelContacts telcontact:telcontacts){
		   ContactsListEntity contactentity=new ContactsListEntity();
		   contactentity.setContact_name(telcontact.getTelcontactsname());
		   L.p(T+"name:"+telcontact.getTelcontactsname());
		   contactentity.setContact_number(telcontact.getTel());
		   contactentity.setContact_photo_url(telcontact.getImage().getImagesrc());
		 
		   listcontact.add(contactentity);
	   }
	   contactlistsuccess.setList(listcontact);
	   Gson gson=new Gson();
	   String sendjson=gson.toJson(contactlistsuccess);
	   send(sendjson,successparam.getResponse());

	}


	public void executeFailure(FailureParam failureparam) {
		// TODO Auto-generated method stub
      ContactsListFailureResponse contactlistfailure=new ContactsListFailureResponse();
      Gson gson=new Gson();
      String jsonstr=gson.toJson(contactlistfailure);
      String sendjson="";
      try {
  		sendjson=java.net.URLEncoder.encode(jsonstr,"utf-8");
  	} catch (UnsupportedEncodingException e1) {
  		// TODO Auto-generated catch block
  		e1.printStackTrace();
  	}
//  	   java.net.URLDecoder.decode(s)
  		PrintWriter pw;
  		try {
  			failureparam.getResponse().setCharacterEncoding("utf-8");
  			pw = failureparam.getResponse().getWriter();
  		    pw.print(sendjson);
  		} catch (IOException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}

	}

	public void execute(ExecuteParam executeparam) {
		// TODO Auto-generated method stub
		
	}
	public class ContactsListSuccessResponse {
		private int code=306;
		private String msg="Select Success";
		private List<ContactsListEntity> list;
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public List<ContactsListEntity> getList() {
			return list;
		}
		public void setList(List<ContactsListEntity> list) {
			this.list = list;
		}
	}
	public class ContactsListEntity {
		private String contact_name="";
		private String contact_number="";
		private String contact_photo_url="";
		public String getContact_name() {
			return contact_name;
		}
		public void setContact_name(String contact_name) {
			this.contact_name = contact_name;
		}
		 
		public String getContact_photo_url() {
			return contact_photo_url;
		}
		public void setContact_photo_url(String contact_photo_url) {
			this.contact_photo_url = contact_photo_url;
		}
		public String getContact_number() {
			return contact_number;
		}
		public void setContact_number(String contact_number) {
			this.contact_number = contact_number;
		}
	}
	public class ContactsListFailureResponse {
		private int code=-306;
		private String msg="Select failure";
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}

	}
}
